﻿using System;
using System.Linq;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
namespace UniMana.Win.Leadership
{
    public partial class FrmReport_SubSubjectOpenedOfClass : DevExpress.XtraEditors.XtraForm
    {

        private DBMain m_subjectOpened = new DBMain();
        string nullText = "---Chọn tất cả---";
        public FrmReport_SubSubjectOpenedOfClass()
        {
            InitializeComponent();
            InitControlEvent();
            LoadData();
            gctSubjectOpenedOfClass.DataSource = "";
        }

        public void InitControlEvent()
        {
            this.xucBar1.btnPrint_Click += 
                new UniMana.Utils.Controls.xucBar.ButtonClickHandler(this.btnPrint_Click);
        }

        
        public void LoadData()
        {
            lueCatalogue_Department.GetData(
                this.m_subjectOpened.m_DCReadOnly.Catalogue_Departments.Where(
                p => p.StudentManage == true),
                "Catalogue_DepartmentKey",
                "Name");
            lueCatalogue_Course.GetData(
                this.m_subjectOpened.m_DCReadOnly.Catalogue_Courses, 
                "Catalogue_CourseKey",
                "Name");
            gluCatalogue_Major.GetData(
                this.m_subjectOpened.m_DCReadOnly.Catalogue_Majors, 
                "ID", 
                "Name");
            gluCatalogue_Major.EditValue = -1;
            lueCatalogue_Department.EditValue = -1;
            lueCatalogue_Department.GetData(
                m_subjectOpened.m_DCReadOnly.Catalogue_Departments.Where(
                p => p.StudentManage == true), 
                nullText: nullText);
            gluCatalogue_Major.GetData(
                m_subjectOpened.m_DCReadOnly.Catalogue_Majors, 
                nullText: nullText);

        }
        private void GetClass()
        {
            int iCouserKey = this.lueCatalogue_Course.GetIntValue();
            int iDepartmentKey = this.lueCatalogue_Department.GetIntValue();
            int iMajorKey = this.gluCatalogue_Major.GetIntValue();

            bds _bds = new bds();
            _bds.DataSource = 
                m_subjectOpened.m_DCReadOnly.Training_Classes.GetTraining_ClassData(
                iCouserKey,
                iDepartmentKey,iMajorKey);
            lueTraining_Class.GetData(_bds);

        }
        private void btnView_Click(object sender, EventArgs e)
        {
            bds _bds1 = new bds();
           // _bds1.DataSource = 
            //m_subjectOpened.m_DCReadOnly.Training_Classes.GetSubjectOpenedClassStudy1(
            //this.m_subjectOpened.m_DCReadOnly.Training_Subjects,
            //this.m_subjectOpened.m_DCReadOnly.Catalogue_SchoolYears,
            //this.m_subjectOpened.m_DCReadOnly.Catalogue_Terms,
            //this.m_subjectOpened.m_DCReadOnly.Training_Subject_Classes,
            //lueCatalogue_Course.GetIntValue(),
            //this.lueCatalogue_Major.GetIntValue(),
            //lueTraining_Class.GetIntValue()).ToDataTable();
            _bds1.DataSource = 
                m_subjectOpened.m_DCReadOnly.Training_Classes.GetSubjectOfClassData(
                this.m_subjectOpened.m_DCReadOnly.Catalogue_SchoolYears, 
                this.m_subjectOpened.m_DCReadOnly.Catalogue_Terms,
                this.m_subjectOpened.m_DCReadOnly.Training_Subject_Classes,
                this.m_subjectOpened.m_DCReadOnly.Training_Subjects, 
                lueCatalogue_Course.GetIntValue(), 
                this.gluCatalogue_Major.GetIntValue(), 
                lueTraining_Class.GetIntValue()).ToDataTable();
            gctSubjectOpenedOfClass.DataSource = _bds1.DataSource;
            grvSubjectOpenedOfClass.ExpandAllGroups();
            gctSubjectOpenedOfClass.DataSource = "";
              
        }

     
        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            int majorKey = this.gluCatalogue_Major.EditValue.ToInt();
            Catalogue_MajorInfo majorInfo = new Catalogue_MajorInfo(majorKey);
            this.lueCatalogue_Department.EditValue = majorInfo.Properties.Catalogue_DepartmentKey;
            GetClass();
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            this.gluCatalogue_Major.GetData( 
                this.m_subjectOpened.m_DCReadOnly.Catalogue_Majors.GetListByDepartMent(
                this.lueCatalogue_Department.GetIntValue()));
            GetClass();
        }
   
       
         private void btnPrint_Click(object sender, EventArgs e)
         {
             printSubjectOpenedOfClass.ClearDocument();
            
             string  strTenlop = lueTraining_Class.Text.ToUpper();
             this.printSubjectOpenedOfClass.ClearDocument();


             this.printSubjectOpenedOfClass.RtfReportHeader = 
                    @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}
                    }\viewkind4\uc1\pard\qc\b\f0\fs36\par\fs32B\'c1O C\'c1O S\u7888? M\'d4N H\u7884?C lop 
                    \u272?\'c3 H\u7884?C\par\b0\fs20\par\par}";
             this.printSubjectOpenedOfClass.RtfReportHeader = 
                 this.printSubjectOpenedOfClass.RtfReportHeader.Replace(
                 "lop", 
                 UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(strTenlop));
             string ngaythang = 
                 "Tân An," + " ngày" + " " + 
                 DateTime.Now.Day + " " + "tháng" + " " + 
                 DateTime.Now.Month + " " + "năm" + " " + 
                 DateTime.Now.Year;
              string nguoidung = 
                  SysGlobalVar.g_sta_sysUserLogin.Properties.FullName;

              this.printSubjectOpenedOfClass.RtfReportFooter = 
                  this.printSubjectOpenedOfClass.RtfReportFooter.Replace(
                  "nguoilap", 
                  UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(nguoidung));
             this.printSubjectOpenedOfClass.RtfReportFooter = 
                 this.printSubjectOpenedOfClass.RtfReportFooter.Replace(
                 "ngaythang", 
                 UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ngaythang));
             printSubjectOpenedOfClass.ShowPreview();
         }
    }
}